package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiGetConfirmCode;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.widget.CustomToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 忘记密码
 * Created by mcc on 2018-07-17.
 */

public class AtyForgetPassword extends BaseSimpleActivity {
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.etAppAtyForgetPasswordPhone)
    EditText mEtPhone;
    @BindView(R.id.btnAppAtyForgetPasswordGetCode)
    Button mBtnGetCode;
    @BindView(R.id.etAppAtyForgetPasswordConfirmCode)
    EditText mEtConfirmCode;
    @BindView(R.id.btnAppAtyForgetPasswordNext)
    Button mBtnNext;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_find_password;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {

        mBtnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEtPhone.getText().toString())) {
                    CustomToast.showLong("请输入手机号码");
                    mEtPhone.requestFocus();
                    return;
                }

                if (!CommonsToolsHelper.isMobileNum(mEtPhone.getText().toString())){
                    CustomToast.showLong("请输入合法的手机号码");
                    mEtPhone.requestFocus();
                    return;
                }

                getConfirmCode();
            }
        });

        mEtConfirmCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (count > 0 && isGetConfirmCode){
                    mBtnNext.setClickable(true);
                    mBtnNext.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnNext.setClickable(false);
                    mBtnNext.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtySetPassword.class);
                intent.putExtra(MyConstants.PHONE_NUM,mEtPhone.getText().toString());
                intent.putExtra(MyConstants.CONFIRM_CODE,mEtConfirmCode.getText().toString());
                intent.putExtra(MyConstants.JUMP_TYPE,MyConstants.FIND_PASSWORD);
                startActivity(intent);
                finish();
            }
        });

        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private boolean isGetConfirmCode;
    HttpOnNextListener<BaseResultEntity> mConfirmCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            Toast.makeText(mContext, "验证码已发送", Toast.LENGTH_SHORT).show();
            new CountDownTimer(60000,1000){
                @Override
                public void onTick(long l) {
                    mBtnGetCode.setEnabled(false);
                    mBtnGetCode.setText("获取验证码"+l/1000);
                    mBtnGetCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666));
                    isGetConfirmCode = true;
                }

                @Override
                public void onFinish() {
                    mBtnGetCode.setEnabled(true);
                    mBtnGetCode.setText("获取验证码");
                    mBtnGetCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_009944));
                }
            }.start();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void getConfirmCode() {
        ApiGetConfirmCode apiGetConfirmCode = new ApiGetConfirmCode(mConfirmCodeListener,mContext,true,false,true);
        Map<String,Object> map = new HashMap<>();
        map.put("PHONE",mEtPhone.getText().toString());
        map.put("MESSAGE_TYPE",2);
        apiGetConfirmCode.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetConfirmCode);
    }

}
